package com.focus.pay.ali.action.order;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.ali.constant.Constant;
import com.focus.pay.ali.constant.AliChannelType;
import com.focus.pay.ali.constant.AliVersion;
import com.focus.pay.core.enums.Channel;
import com.focus.pay.ali.model.order.AliPayModelApp;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;

/**
 * @Author: 赵参谋
 * @Date: 2021/9/29 15:07
 * @Description:APP支付
 * 适用于APP站内支付
*/
@PayOrderHandler(channel = Channel.AliPay,channelType = AliChannelType.App,version = AliVersion.VERSION_2)
public class AliPayOrderApp extends AliPayOrder{

    public AlipayTradeAppPayResponse payOrder(AliPayModelApp aliPayModelApp, AliPayConfig aliPayConfig) throws PayException {

        try {
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            request.setNotifyUrl(aliPayModelApp.getNotifyUrl());
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelApp));
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            return response;
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }
}
